/*
 * Zenlayer.com Inc.
 * Copyright (c) 2014-2021 All Rights Reserved.
 */
package com.shen.integration.impl;

import com.shen.integration.RabbitMQService;
import com.shen.integration.callback.RabbitConfirmCallback;
import com.shen.integration.callback.RabbitReturnCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Objects;
import java.util.UUID;

/**
 * @author wangjunjie
 * @version $ Id: RabbitMqServiceImpl.java, v 0.1  wangjunjie Exp $
 * @date 2019-09-18 14:58:02
 */
@Service
public class RabbitMqServiceImpl implements RabbitMQService {
    private static final Logger logger = LoggerFactory.getLogger(RabbitMqServiceImpl.class);

    @Resource
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void initRabbitTemplate() {
        // 设置生产者消息确认
        rabbitTemplate.setConfirmCallback(new RabbitConfirmCallback());
        rabbitTemplate.setReturnsCallback(new RabbitReturnCallback());
    }

    @Override
    public void sendMessage(String exchange, String routingKey, Object message) {
        if (Objects.nonNull(message)) {
            rabbitTemplate.convertAndSend(exchange, routingKey, message, new CorrelationData("Routing-" + UUID.randomUUID()));
            logger.info("发送一条消息,exchange:[{}],routingKey:[{}],message:[{}]", exchange, routingKey, message);
        }
    }
}
